<?php
//display quote 
Yii::app()->clientScript->registerScriptFile(Yii::app()->baseUrl . '/js/app/salesOrderConvertUtil.js', CClientScript::POS_BEGIN);

$countQuote = count($quoteList);

$table = new CI_Table();
$tmpl = array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
);
$table->set_template($tmpl);
$table->set_heading(array(
    'Date', 'Reference', 'Quote No', 'Status', 'Amount'
));
echo "<br/><br/><h4>QUOTES</h4><br/>";
$quote_total = 0;
if ($countQuote > 0) {
    for ($i = 0; $i < $countQuote; $i++) {

        $item = $quoteList[$i];
        /* @var $item Invoice */

        $data = array(
            $item->invoicedate,
            NULL,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            $item->getQuoteStatusText(),
            '<div style="text-align: right;">' . number_format($item->getAmountWithDiscount(), 2) . '<div>',

        );
        $quote_total += $item->getAmountWithDiscount();
        $table->add_row($data);
    }
    $table->add_row(array('<br/>', NULL, NULL, NULL, NULL));
    $data = array(
        'Total',
        NULL,
        NULL,
        NULL,
        '<div style="text-align: right;" >' . number_format($quote_total, 2) . '<div>',

    );
    $table->add_row($data);
    echo $table->generate();
} else {
    echo "No quote have been created with this job number.";
}
?>

<br/><h4>SALES ORDERS</h4> <br/>
<?php
if (isset($jobModel))
    $refacc = $jobModel->customer->quickbook . ':' . $jobModel->jobid;
$soCount = count($salesOrderList);


$table->set_heading(array(
    'Date', 'Reference', 'Sales Order No', 'Status', 'Amount', 'Deposits',
));
$table->set_template(array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
));
if ($soCount > 0) {
    $convertUtil = new SalesOrderPaymentConvertUtil();
    if (isset($jobModel)) {
        $criteria = new CDbCriteria();
        $criteria->condition = "customerRefFullName='$refacc'";
        $criteria->order = "date Asc";
        $paymentsApplied = Yii::app()->dbqb->createCommand('select *
          from journalentry
          INNER JOIN journalcreditlinedetail
ON journalentry.txnid = journalcreditlinedetail.idkey
                        where EntityRef_FullName  =:refacc ')->bindValues(array(
            'refacc' => $refacc,
        ))->queryAll();
        ;
        $paymentList = $paymentsApplied;

        $depositsList = Yii::app()->dbqb->createCommand('SELECT *
            FROM cc_quickbooks.receivepayment
WHERE CustomerRef_FullName =:refacc ')->bindValues(array(
            'refacc' => $refacc,
        ))->queryAll();
        ;

    } else {
        $paymentList = array();
        $depositsList = array();
    }
    $paymentStr = '';
    $so_total = 0;
    $depositsStr = '';
    $paymentStr - '';
    foreach ($salesOrderList as $item) {
        $so_total += $item->getAmountWithDiscount();
        $depositsAmount = 0;
        if (count($depositsList) > 0) {
            $dep_total = 0;
            $depositsStr = '';
            foreach ($depositsList as $payment) {
                $dep_total += $payment['TotalAmount'];
                $no = str_replace('2012-', '', $item->invoice);

                //generate the table
                $payTable = new CI_Table;
                $payTable->set_template(array(
                    'table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="" width="200">',
                ));

                //can apply to invoice, must have invoice to apply
                $paymentRef = $payment['TxnNumber'];
                $paymentAmount = $payment['TotalAmount'];
                $paymentMemo = $payment['Memo'];
                $paymentDate = $payment['TimeCreated'];

                $payTable->add_row(array('Memo :'. $paymentMemo));
                //register the tooltip script

                // if(strpos($payment->memo, $no)){
                if (strpos($payment['Memo'], $no)) {
                    Yii::app()->clientScript->registerScript("payment-$paymentRef", "
                                        $('.payment-$paymentRef').hide();
                                        $('#payment-$paymentRef').qtip({
                                            content:$('.payment-$paymentRef')
                                            });
                                        ");


                    $depositsStr .= <<<HERE
                                        <a href="#" id='payment-$paymentRef'>$paymentDate -Payment - $paymentAmount</a>
                                        <div  class='payment-$paymentRef'>
                                            {$payTable->generate()}
                                        </div><br/>


HERE;
                }

            }
        }
        if (count($paymentList) > 0) {
            $pay_total = 0;
            $paymentStr = '';
            foreach ($paymentList as $payment) {
                $pay_total += $payment['Amount'];
                $no = str_replace('2012-', '', $item->invoice);

                //generate the table
                $payTable = new CI_Table;
                $payTable->set_template(array(
                    'table_open' => '<table border="0" cellpadding="0" cellspacing="0" class="" width="200">',
                ));

                //can apply to invoice, must have invoice to apply
                $paymentRef = $payment['TxnLineID'];
                $paymentAmount = $payment['Amount'];
                $paymentMemo = $payment['Memo'];
                $paymentDate = $payment['TimeCreated'];
                $paymentDate = explode(' ',$paymentDate);

                $paymentDate = $paymentDate[0];
                $payTable->add_row(array('Memo :'. $paymentMemo));
                //register the tooltip script

                 if(true){
               // if (strpos($payment['Memo'], $no)) {
                    Yii::app()->clientScript->registerScript("payment-$paymentRef", "
                                        $('.payment-$paymentRef').hide();
                                        $('#payment-$paymentRef').qtip({
                                            content:$('.payment-$paymentRef')
                                            });
                                        ");


                     $paymentStr .= <<<HERE
                                        <a href="#" id='payment-$paymentRef'>$paymentDate - General Journal - $paymentAmount</a>
                                        <div  class='payment-$paymentRef'>
                                            {$payTable->generate()}
                                        </div><br/>


HERE;
                }

            }
        }

        $data = array(
            $item->invoicedate,
            $item->quoteorg,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            JobsStatus::getText($item->salesorderstatus),
            '<div style="text-align: right;" >' . number_format($item->getAmountWithDiscount(), 2) . '<div>',
            $depositsStr .$paymentStr
        );
        $table->add_row($data);
    }
    $table->add_row(array('<br/>', NULL, NULL, NULL, NULL, NULL));
         $total_payments = 0;
    if(isset($dep_total)) $total_payments +=$dep_total;
    if(isset($pay_total)) $total_payments +=$pay_total;
    $data = array(
        'Total',
        NULL,
        NULL,
        NULL,

        '<div style="text-align: right;" >' . number_format($so_total, 2) . '<div>',
        '<div style="text-align: right;" >' . number_format($total_payments, 2) . '<div>',

    );
    $table->add_row($data);
    echo $table->generate();
} else {
    echo 'No sales orders have been created with this job number.';
}
?>
<br/>
<h4>INVOICES & CREDIT MEMOS</h4><br/>
<?php
$invCount = count($invList);
$table->set_template(array(
    'table_open' => '<table border="1" cellpadding="2" cellspacing="1" class="" width="500">',
    'row_start' => '<tr style="vertical-align: top;">'
));
$table->set_heading(array(
    'Type', 'Date', 'Reference', 'Number', 'Amount', 'Payments'
));
if ($invCount > 0) {

    $inv_total = 0;
    $inv_payments_total = 0;
    foreach ($invList as $item) {
        /**@var $item Invoice */
        $criteria = new CDbCriteria();
        $criteria->condition = "invoiceNo='$item->invoice'";
        $criteria->order = "date Asc";
        $paymentsApplied = QbPaymentsapplied::model()->findAll($criteria);
        $paymentList = $paymentsApplied;
        $paymentStr = '';


        if ($item->invoicetype == Invoice::TYPE_CREDIT_MEMO) {
            $reference = $item->invoiceorg;
            $itemStatus = '';
        } else {
            $reference = $item->salesordersorg;
            $itemStatus = $item->getQuoteStatusText();
        }
        $data = array(
            $item->invoicetype,
            $item->invoicedate,
            $reference,
            "<a href='index.php?r=invoices/update&invoice=$item->invoice'>$item->invoice</a>",
            '<div style="text-align: right;" >' . number_format($item->getAmountWithDiscount(), 2) . '</div>',
            '<div style="text-align: right;" >' . number_format($item->getPaymentApplied(), 2) . '</div>',
        );
        $table->add_row($data);
        $inv_total += $item->getAmountWithDiscount();
        $inv_payments_total += $item->getPaymentApplied();
    }
    $table->add_row(array('<br/>', NULL, NULL, NULL, NULL, NULL));
    $data = array(
        'Total',
        NULL,
        NULL,

        NULL,
        '<div style="text-align: right;" >' . number_format($inv_total, 2) . '<div>',
        '<div style="text-align: right;" >' . number_format($inv_payments_total, 2) . '<div>',

    );
    $table->add_row($data);
    echo $table->generate();
} else {
    echo 'No invoice have been created with this job number.';
}
?>
